package com.yunji.framework_template.common.http;

import java.io.IOException;
import java.util.Map;

import org.apache.commons.httpclient.HttpException;
import org.apache.http.client.ClientProtocolException;

import com.yunji.framework_template.common.web.HttpResult;

/**
 * HttpClient工具类
 * 
 * @author fenglibin
 * @date 2015-2-5
 */
public class HttpClientUtil {

    /**
     * 发起Post请求，并返回SdkHttpResult对象
     * 
     * @param url 请求的URL地址
     * @param content 发送的内容
     * @return
     * @throws HttpException
     * @throws IOException
     */
    public static HttpResult doPost(String url, String content) throws HttpException, IOException {
        return doPost(url, content, null);

    }

    /**
     * 发起Post请求，并返回SdkHttpResult对象
     * 
     * @param url 请求的URL地址
     * @param content 发送的内容
     * @param header 请求头
     * @return
     * @throws HttpException
     * @throws IOException
     */
    public static HttpResult doPost(String url, String content, Map<String, String> header) throws HttpException,
                                                                                            IOException {
        return HttpConnection.doPost(url, content, header);
    }

    /**
     * 发起Post请求，并返回SdkHttpResult对象
     * 
     * @param url 请求的URL地址
     * @param param 发送的内容
     * @param header 请求头
     * @return
     * @throws HttpException
     * @throws IOException
     */
    public static HttpResult doPost(String url, Map<String, String> param) throws HttpException, IOException {
        return doPost(url, param, null);
    }

    /**
     * 发起Post请求，并返回SdkHttpResult对象
     * 
     * @param url 请求的URL地址
     * @param param 发送的内容
     * @param header 请求头
     * @return
     * @throws HttpException
     * @throws IOException
     */
    public static HttpResult doPost(String url, Map<String, String> param,
                                    Map<String, String> header) throws HttpException, IOException {
        return HttpConnection.doPost(url, param, header);
    }

    /**
     * 通过get方法获取网页内容
     * 
     * @param url 获取内容的URL
     * @param resultWithHeader 响应的内容是否带响应头
     * @return 根据当前的URL，获取到的网页的内容
     * @throws IOException
     * @throws HttpException
     */
    public static HttpResult doGet(String url) throws HttpException, IOException {
        return HttpConnection.doGet(url);
    }

    public static HttpResult doGet(String url, Map<String, String> param) throws HttpException, IOException {
        return HttpConnection.doGet(url, param);
    }

    public static HttpResult doGet(String url, Map<String, String> param,
                                   Map<String, String> header) throws HttpException, IOException {
        return HttpConnection.doGet(url, param, header);
    }

    /**
     * 检查当前URL是否可以成功访问
     * 
     * @param url
     * @param param
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static HttpResult checkUrl(String url, Map<String, String> param) throws ClientProtocolException,
                                                                             IOException {
        return HttpConnection.checkUrl(url, param);
    }
}
